/**
 * Created with IntelliJ IDEA.
 * User: ajhin
 * Date: 3/9/14
 * Time: 5:34 PM
 * To change this template use File | Settings | File Templates.
 */
package societyGames
{
import com.societyGames.sortEngine.SortItem;
import com.societyGames.sortEngine.SortScene;

import flexunit.framework.Assert;

public class SortTest
{

  [Test]
  public function simpleAdd():void
  {
    var sortScene:SortScene = new SortScene();

    var item3:SortItem = new SortItem();
    item3.zSort = 3;

    var item2:SortItem = new SortItem();
    item2.zSort = 2;

    var item1:SortItem = new SortItem();
    item1.zSort = 1;

    sortScene.addItem(item3);
    sortScene.addItem(item2);
    sortScene.addItem(item1);

    Assert.assertEquals(item1, sortScene.sprite.getChildAt(0));
    Assert.assertEquals(item2, sortScene.sprite.getChildAt(1));
    Assert.assertEquals(item3, sortScene.sprite.getChildAt(2));
    Assert.assertTrue(isOrderCorrect(sortScene));
    Assert.assertTrue(isIndexCorrect(sortScene));

    sortScene = new SortScene();
    sortScene.addItem(item3);
    sortScene.addItem(item1);
    sortScene.addItem(item2);

    Assert.assertEquals(item1, sortScene.sprite.getChildAt(0));
    Assert.assertEquals(item2, sortScene.sprite.getChildAt(1));
    Assert.assertEquals(item3, sortScene.sprite.getChildAt(2));
    Assert.assertTrue(isOrderCorrect(sortScene));
    Assert.assertTrue(isIndexCorrect(sortScene));

    sortScene = new SortScene();
    sortScene.addItem(item2);
    sortScene.addItem(item1);
    sortScene.addItem(item3);

    Assert.assertEquals(item1, sortScene.sprite.getChildAt(0));
    Assert.assertEquals(item2, sortScene.sprite.getChildAt(1));
    Assert.assertEquals(item3, sortScene.sprite.getChildAt(2));
    Assert.assertTrue(isOrderCorrect(sortScene));
    Assert.assertTrue(isIndexCorrect(sortScene));

    sortScene = new SortScene();
    sortScene.addItem(item2);
    sortScene.addItem(item3);
    sortScene.addItem(item1);

    Assert.assertEquals(item1, sortScene.sprite.getChildAt(0));
    Assert.assertEquals(item2, sortScene.sprite.getChildAt(1));
    Assert.assertEquals(item3, sortScene.sprite.getChildAt(2));
    Assert.assertTrue(isOrderCorrect(sortScene));
    Assert.assertTrue(isIndexCorrect(sortScene));

    sortScene = new SortScene();
    sortScene.addItem(item1);
    sortScene.addItem(item2);
    sortScene.addItem(item3);

    Assert.assertEquals(item1, sortScene.sprite.getChildAt(0));
    Assert.assertEquals(item2, sortScene.sprite.getChildAt(1));
    Assert.assertEquals(item3, sortScene.sprite.getChildAt(2));
    Assert.assertTrue(isOrderCorrect(sortScene));
    Assert.assertTrue(isIndexCorrect(sortScene));

    sortScene = new SortScene();
    sortScene.addItem(item1);
    sortScene.addItem(item3);
    sortScene.addItem(item2);

    Assert.assertEquals(item1, sortScene.sprite.getChildAt(0));
    Assert.assertEquals(item2, sortScene.sprite.getChildAt(1));
    Assert.assertEquals(item3, sortScene.sprite.getChildAt(2));
    Assert.assertTrue(isOrderCorrect(sortScene));
    Assert.assertTrue(isIndexCorrect(sortScene));
  }

  [Test]
  public function simpleMove():void
  {
    var sortScene:SortScene;
    var item3:SortItem = new SortItem();
    var item2:SortItem = new SortItem();
    var item1:SortItem = new SortItem();

    item3.zSort = 0;
    item2.zSort = 0;
    item1.zSort = 0;

    sortScene = new SortScene();

    sortScene.addItem(item3);
    Assert.assertTrue(isOrderCorrect(sortScene));
    Assert.assertTrue(isIndexCorrect(sortScene));

    sortScene.addItem(item2);
    Assert.assertTrue(isOrderCorrect(sortScene));
    Assert.assertTrue(isIndexCorrect(sortScene));

    sortScene.addItem(item1);
    Assert.assertTrue(isOrderCorrect(sortScene));
    Assert.assertTrue(isIndexCorrect(sortScene));

    item3.zSort = 3;
    Assert.assertEquals(item3, sortScene.sprite.getChildAt(2));
    Assert.assertTrue(isOrderCorrect(sortScene));
    Assert.assertTrue(isIndexCorrect(sortScene));

    item2.zSort = 2;
    Assert.assertEquals(item2, sortScene.sprite.getChildAt(1));
    Assert.assertEquals(item3, sortScene.sprite.getChildAt(2));
    Assert.assertTrue(isOrderCorrect(sortScene));
    Assert.assertTrue(isIndexCorrect(sortScene));

    item1.zSort = 1;
    Assert.assertEquals(item1, sortScene.sprite.getChildAt(0));
    Assert.assertEquals(item2, sortScene.sprite.getChildAt(1));
    Assert.assertEquals(item3, sortScene.sprite.getChildAt(2));
    Assert.assertTrue(isOrderCorrect(sortScene));
    Assert.assertTrue(isIndexCorrect(sortScene));

    item3.zSort = -1;
    Assert.assertTrue(isOrderCorrect(sortScene));
    Assert.assertTrue(isIndexCorrect(sortScene));

    item2.zSort = -2;
    Assert.assertTrue(isOrderCorrect(sortScene));
    Assert.assertTrue(isIndexCorrect(sortScene));

    item2.zSort = -3;
    Assert.assertTrue(isOrderCorrect(sortScene));
    Assert.assertTrue(isIndexCorrect(sortScene));
  }

  [Test]
  public function simpleRemove():void
  {
    var sortScene:SortScene = new SortScene();
    var item3:SortItem = new SortItem();
    var item2:SortItem = new SortItem();
    var item1:SortItem = new SortItem();

    item3.zSort = 3;
    item2.zSort = 2;
    item1.zSort = 1;

    sortScene.addItem(item3);
    Assert.assertTrue(isOrderCorrect(sortScene));
    Assert.assertTrue(isIndexCorrect(sortScene));

    sortScene.addItem(item2);
    Assert.assertTrue(isOrderCorrect(sortScene));
    Assert.assertTrue(isIndexCorrect(sortScene));

    sortScene.addItem(item1);
    Assert.assertTrue(isOrderCorrect(sortScene));
    Assert.assertTrue(isIndexCorrect(sortScene));

    sortScene.removeItem(item2);

    Assert.assertTrue(isOrderCorrect(sortScene));
    Assert.assertTrue(isIndexCorrect(sortScene));
    Assert.assertEquals(item1, sortScene.sprite.getChildAt(0));
    Assert.assertEquals(item3, sortScene.sprite.getChildAt(1));

    sortScene.addItem(item2);
    Assert.assertEquals(item1, sortScene.sprite.getChildAt(0));
    Assert.assertEquals(item2, sortScene.sprite.getChildAt(1));
    Assert.assertEquals(item3, sortScene.sprite.getChildAt(2));
    Assert.assertTrue(isOrderCorrect(sortScene));
    Assert.assertTrue(isIndexCorrect(sortScene));

    var newSortScene:SortScene = new SortScene();

    newSortScene.addItem(item2);
    Assert.assertEquals(item2, newSortScene.sprite.getChildAt(0));
    Assert.assertEquals(item3, sortScene.sprite.getChildAt(1));
    Assert.assertEquals(item1, sortScene.sprite.getChildAt(0));
    Assert.assertTrue(isOrderCorrect(sortScene));
    Assert.assertTrue(isIndexCorrect(sortScene));
    Assert.assertTrue(isOrderCorrect(newSortScene));
    Assert.assertTrue(isIndexCorrect(newSortScene));

    newSortScene.addItem(item3);
    Assert.assertEquals(item2, newSortScene.sprite.getChildAt(0));
    Assert.assertEquals(item3, newSortScene.sprite.getChildAt(1));
    Assert.assertEquals(item1, sortScene.sprite.getChildAt(0));
    Assert.assertTrue(isOrderCorrect(sortScene));
    Assert.assertTrue(isIndexCorrect(sortScene));
    Assert.assertTrue(isOrderCorrect(newSortScene));
    Assert.assertTrue(isIndexCorrect(newSortScene));

    newSortScene.addItem(item1);
    Assert.assertEquals(item2, newSortScene.sprite.getChildAt(1));
    Assert.assertEquals(item3, newSortScene.sprite.getChildAt(2));
    Assert.assertEquals(item1, newSortScene.sprite.getChildAt(0));
    Assert.assertEquals(0, sortScene.sprite.numChildren);
    Assert.assertTrue(isOrderCorrect(sortScene));
    Assert.assertTrue(isIndexCorrect(sortScene));
    Assert.assertTrue(isOrderCorrect(newSortScene));
    Assert.assertTrue(isIndexCorrect(newSortScene));
  }

  private function isOrderCorrect(sortScene:SortScene):Boolean
  {
    var lastZSort:Number = Number.NEGATIVE_INFINITY;
    for (var index:int = 0; index < sortScene.sprite.numChildren; index++)
    {
      var sortItem:SortItem = SortItem(sortScene.sprite.getChildAt(index));
      if (sortItem.zSort < lastZSort)
      {
        return false;
      }
      lastZSort = sortItem.zSort;
    }
    return true;
  }

  private function isIndexCorrect(sortScene:SortScene):Boolean
  {
    for (var index:int = 0; index < sortScene.sprite.numChildren; index++)
    {
      var sortItem:SortItem = SortItem(sortScene.sprite.getChildAt(index));
      if (sortItem.index != index)
      {
        return false;
      }
    }
    return true;
  }

}
}
